﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace WowGuildSite.Armory
{
    public enum SkillCategory
    {
        Profession,
        Secondary,
        Weapon,
        Class,
        Armor,
        Language,
    }

    public enum SkillKey
    {
        // Professions:
        Skinning,
        Leatherworking,
        Mining,
        Blacksmithing,
        Alchemy,
        Herbalism,
        Enchanting,
        Tailoring,
        Engineering,
        Jewelcrafting,
        
        // Secondaries:
        Fishing,
        FirstAid,
        Cooking,
        Riding,

        // Weapons:
        CrossBows,
        Bows,
        Daggers,
        Guns,
        Swords,
        Polearms,
        Maces,
        Staves,
        Unarmed,
        Defense,
        Thrown,
        TwoHandedSwords,
        TwoHandedAxes,
        TwoHandedMaces,
        Axes,
        Wands,

        // Class: Rogue
        Poisons,
        Combat,
        Assassination,
        LockPicking,
        Subtlety,

        // Class: Hunter
        Fury,
        Protection,
        Arms,
        BeastMastery,
        Marksmanship,
        Survival,

        // Class: Druid
        Balance,
        Restoration,
        FeralCombat,

        // Class: Mage
        Arcane,
        Frost,
        Fire,

        // Class: Priest
        Discipline,
        Holy,
        ShadowMagic,

        // Class: Shaman
        ElementalCombat,
        Enhancement,

        // Class: Warlock
        Demonology,
        Affliction,
        Destruction,

        // Class: Paladin
        Retribution,

        // Armor:
        Cloth,
        Leather,
        Mail,
        PlateMail,
        Shield,

        // Languages:
        GutterSpeak,
        Orcish,
        Taurahe,
        Thalassian,
        Troll,
    }

    public class ArmorySkill 
    {
        private SkillCategory   category;
        private SkillKey        key;
        private string          localizedName;
        private string          localizedCategory;
        private int             max;
        private int             current;

        public SkillKey         Key
        {
            get { return key; }
        }

        public SkillCategory    Category
        {
            get { return category; }
        }

        public string           LocalizedName
        {
            get { return localizedName; }
        }

        public string           LocalizedCategory
        {
            get { return localizedCategory; }
        }

        public int              Max
        {
            get { return max; }
        }

        public int              Current
        {
            get { return current; }
        }

        internal static ArmorySkill FromElement(XmlElement skillElement )
        {
            if (skillElement == null)
            {
                return new ArmorySkill();
            }

            XmlElement categoryElement = (XmlElement)skillElement.ParentNode;
            
            // Get the skill key, and modify it so it matches our defined enum values:
            string keyName = ArmoryXmlUtility.GetElementAttributeText(skillElement, "key");

            // Remove any "-":
            keyName = keyName.Replace("-","");
            // Remove language prefix:
            keyName = keyName.Replace("language:","");
            
            ArmorySkill skill   = new ArmorySkill();
            skill.key           = (SkillKey)Enum.Parse(typeof(SkillKey),keyName,true);
            skill.max           = ArmoryXmlUtility.GetElementAttributeInt(skillElement, "max");
            skill.current       = ArmoryXmlUtility.GetElementAttributeInt(skillElement, "value");
            skill.localizedName = ArmoryXmlUtility.GetElementAttributeText(skillElement, "name");

            if (skill.localizedName.Contains(":"))
            {
                // Remove everything before the ":"
                int position = skill.localizedName.IndexOf(':');
                skill.localizedName = skill.localizedName.Substring(position +2);
            }

            // Get the category data:
            string categoryKey = ArmoryXmlUtility.GetElementAttributeText(categoryElement, "key");

            switch (categoryKey)
            {
                case "professions":
                    skill.category = SkillCategory.Profession;
                    break;
                case "secondaryskills":
                    skill.category = SkillCategory.Secondary;
                    break;
                case "weaponskills":
                    skill.category = SkillCategory.Weapon;
                    break;
                case "classskills":
                    skill.category = SkillCategory.Class;
                    break;
                case "armorproficiencies":
                    skill.category = SkillCategory.Armor;
                    break;
                case "languages":
                    skill.category = SkillCategory.Language;
                    break;
            }

            skill.localizedCategory = ArmoryXmlUtility.GetElementAttributeText(categoryElement, "name");

            return skill;
        }

        private ArmorySkill()
        {
        }
    }
}
